﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="changePasswordform.aspx.cs" Inherits="KPIReport.Web.Forms.Administration.UserManagement.changePasswordform" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Change Password Form</title>

    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.sha1.js") %>" type="text/javascript"></script>
    
    <script type="text/javascript">

        $("#btnSave").button({ icons: { primary: "ui-icon-disk" } });
        var MenuForgotPass = '<%= Request.Params["menuforgotpass"] %>';
        $('#<%= hfIsForgotPassword.ClientID %>').val(MenuForgotPass);
        
    </script>    
    <script type="text/javascript">

        $("#btnClose").button({ icons: { primary: "ui-icon-close" } });
        if ($('#<%= hfIsForgotPassword.ClientID %>').val().toString() == "") {
                $('#btnClose').click(function () {
                    parent.$('#dialogPassword').dialog('close');

                    return false;

                });
            }
    </script>


    <script type="text/javascript">

        $(document).ready(function () {

            var userId = '<%= base.UserID %>';

            $.ajax({
                type: 'POST',
                url: '<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sUserhandler.ashx") %>?method=getpassword&id=' + userId,
                contentType: 'application/json',
                dataType: 'json',
                success: function (data) {

                    $('#txtOldPasswordHashed').val(data.Password);

                },
                error: function () {
                    alert('Error!');
                }
            });

            jQuery.validator.addMethod("notEqualTo", function (value, element, param) {
                var notEqual = true;
                value = $.trim(value);
                for (i = 0; i < param.length; i++) {
                    if (value == $.trim($(param[i]).val())) { notEqual = false; }
                }
                return this.optional(element) || notEqual;
            }, "Password baru tidak boleh sama dengan password lama");

            jQuery.validator.addMethod("cekPassword", function (value, element, param) {
                var notEqual = false;
                value = $.trim(value);
                for (i = 0; i < param.length; i++) {
                    if (value == $.trim($(param[i]).val())) { notEqual = true; }
                }
                return this.optional(element) || notEqual;
            }, "Password lama tidak sama dengan password yang tersimpan dalam database, ulangi kembali");

            // 1. prepare the validation rules and messages.
            var rules = {
                txtPassword: {
                    required: true,
                    maxlength: 50
                },
                txtPasswordLama :{
                    required: true,
                    maxlength: 50
                },
                txtVerifyPassword: {
                    required: true,
                    maxlength: 50,
                    equalTo: "#txtPassword"
                },
                txtPasswordLamaHashed: {
                    cekPassword: ['#txtOldPasswordHashed']
                },
                txtPasswordHashed: {
                    notEqualTo: ['#txtOldPasswordHashed']
                }

            };
            var messages = {
                txtPassword: {
                    required: "Password wajib di isi",
                    maxlength: "Panjang maksimal 'password' adalah 50"
                },
                txtPasswordLama: {
                    required: "Password lama wajib di isi",
                    maxlength: "Panjang maksimal 'password lama' adalah 50"
                },
                txtVerifyPassword: {
                    required: "Ulangi Password wajib di isi",
                    maxlength: "Panjang maksimal 'Ulangi password' adalah 50",
                    equalTo: "Ulangi password harus sama dengan password baru"
                }

            };

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);

            // 3. Set the click event to do the validation
            $("#btnSave").click(function () {

                if (!validator.validate())
                    return ;
                
                    var password = $('#txtPassword').val();

                    var param = 'id=' + userId + '&password=' + password;

                    CallAjax('<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sUserhandler.ashx") %>', '?method=changepassword&' + param);

                    parent.$('#dialogPassword').dialog('close');

                    return false;
                
            });

        });

    </script>
      <script type="text/javascript">

          //Match 6 to 10 character string with at least one upper case letter, one lower case letter, and one digit (useful for passwords).
          var passwordStrengthRegex = /((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,10})/gm;
          var HasError = false;

          $(function () {
              var txtPassword = $("#txtPassword");

              txtPassword.blur(function () {
                  if (txtPassword.val() != "") {
                      var match = String($(this).val()).match(passwordStrengthRegex);
                      if (!match) {
                          $("#txtPassword").focus();
                          $("#txtPassword").val('');
                          $("#txtPassword").css({ 'background-color': 'red' });
                          //                    alert("Password must 6 to 15 character string with at least one upper case letter, one lower case letter, and one digit");
                          $("#msgErrorChangePassword").show();
                          $("#msgErrorChangePassword").html("Password baru harus 6 sampai 10 karakter \n dengan setidaknya satu huruf besar, satu huruf kecil, dan satu digit angka.");
                          HasError = true;
                      }
                      else {
                          $("#txtPassword").css({ 'background-color': '' });
                          HasError = false;
                          $("#msgErrorChangePassword").html("");
                          $("#msgErrorChangePassword").hide();
                      }
                  }
              });
          });
    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            $('#txtPassword').focusout(function () {

                var password = $(this).val();
                var hashedPwd = $.sha1(password);
                $('#txtPasswordHashed').val(hashedPwd.toString().toUpperCase());

                return false;
            });
        });

            $(document).ready(function () {
            $('#txtPasswordLama').focusout(function () {

                var passwordLama = $(this).val();
                var hashedPwdLama = $.sha1(passwordLama);
                $('#txtPasswordLamaHashed').val(hashedPwdLama.toString().toUpperCase());

                return false;
            });

        });

        </script>

</head>
<body>

    <form id="form1" runat="server" >
    <div>
        <table border="0" cellpadding="1" cellspacing="1">
            <tr>
                <td>
                    Nama User
                </td>
                <td style="padding-left: 20px">
                    :
                </td>
                <td style="padding-left: 20px">
                    <asp:TextBox ID="txtUserName" runat="server" Enabled="false" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Password Lama</td>
                <td style="padding-left: 20px">
                    :</td>
                <td style="padding-left: 20px">
                    <asp:TextBox ID="txtPasswordLama" runat="server" TextMode="Password" MaxLength="50" Width="200px"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
                </td>
            </tr>
            <tr>
                <td>
                    Password Baru
                </td>
                <td style="padding-left: 20px">
                    :
                </td>
                <td style="padding-left: 20px">
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" MaxLength="50" Width="200px"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
                </td>
            </tr>
            <tr>
                <td>
                    Ulangi Password
                </td>
                <td style="padding-left: 20px">
                    :
                </td>
                <td style="padding-left: 20px">
                    <asp:TextBox ID="txtVerifyPassword" runat="server" TextMode="Password" MaxLength="50" Width="200px"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
                </td>
            </tr>
            <tr>
            <td colspan="3">
                <div id="msgErrorChangePassword" style="color: Red; font: 11px Arial,Helvetica,sans-serif;
                    height: 19px; width: 340px;">
                </div>
            </td></tr>
        </table>
    </div>
    <br />
    <asp:HiddenField ID="hfIsForgotPassword" runat="server" />
    <br />
    <div>
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td align="right">
                    <button id="btnSave" type="button">Save</button>
                     <button id="btnClose" type="button">Batal</button>
                    <asp:TextBox ID="txtOldPasswordHashed" style="visibility:hidden" runat="server" Width="10px"></asp:TextBox>
                    <asp:TextBox ID="txtPasswordHashed" style="visibility:hidden" runat="server" Width="10px"></asp:TextBox>
                    <asp:TextBox ID="txtPasswordLamaHashed" style="visibility:hidden" runat="server" Width="10px"></asp:TextBox>
                </td>
            </tr>
            
        </table>
    </div>
    </form>
</body>
</html>
